package com.amit.servlet;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class LoginAction extends JLCBaseAction {
	
	
	Logger log=Logger.getLogger(LoginAction.class);
	
	@Override
	public String service(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.info("LoginAction-service()");
		
		String un=request.getParameter("username");
		String pw=request.getParameter("password");
		String result="index.jsp";
		
		log.info("un:"+un+"\t"+un.length());
		log.info("pw:"+pw);
		boolean error=false;
		
		if(JLCValidator.validateRequired(un))
		{
			request.setAttribute("UNMERR", "Username is Required");
			error=true;
		}
		if(JLCValidator.validateRequired(pw))
		{
			request.setAttribute("PWDERR", "Password is Required");
			error=true;
		}
		
		if(error)
		{
			request.setAttribute("UNM", un);
			return result;
		}
		
		int x=userService.verifyUser(un,pw);
		HttpSession session=request.getSession();
		if(x==1)
		{
			session.setAttribute("UN", un);
			result="home.jsp";
		}
		else
		{
			request.setAttribute("MSG", "Invalid Username or password");
		}
		
		return result;

	}
}
